package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ChoiceActivity;
import flc.ast.activity.RecommendActivity;
import flc.ast.activity.WallpaperDetailActivity;
import flc.ast.adapter.ChoiceAdapter;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.FragmentWallpaperBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WallpaperFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WallpaperFragment extends BaseNoModelFragment<FragmentWallpaperBinding> {

    private List<StkTagResBean> listType = new ArrayList<>();
    private RecommendAdapter recommendAdapter;
    private ChoiceAdapter choiceAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_wallpaper;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivWallpaperType1.setOnClickListener(this);
        mDataBinding.ivWallpaperType2.setOnClickListener(this);
        mDataBinding.ivWallpaperType3.setOnClickListener(this);
        mDataBinding.ivRecommend.setOnClickListener(this);
        mDataBinding.tvChoiceAll.setOnClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);

        mDataBinding.rvChoiceList.setLayoutManager(new GridLayoutManager(mContext, 2));
        choiceAdapter = new ChoiceAdapter();
        mDataBinding.rvChoiceList.setAdapter(choiceAdapter);
        choiceAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivWallpaperType1://黑白
                ChoiceActivity.type = 0;
                ChoiceActivity.url = AppConfig.URL_PREFIX + listType.get(0).getHashid();
                startActivity(ChoiceActivity.class);
                break;
            case R.id.ivWallpaperType2://二次元
                ChoiceActivity.type = 1;
                ChoiceActivity.url = AppConfig.URL_PREFIX + listType.get(1).getHashid();
                startActivity(ChoiceActivity.class);
                break;
            case R.id.ivWallpaperType3://可爱
                ChoiceActivity.type = 2;
                ChoiceActivity.url = AppConfig.URL_PREFIX + listType.get(2).getHashid();
                startActivity(ChoiceActivity.class);
                break;
            case R.id.ivRecommend://每日推荐
                startActivity(RecommendActivity.class);
                break;
            case R.id.tvChoiceAll://精选热门全部
                ChoiceActivity.type = 3;
                ChoiceActivity.url = AppConfig.CHOICE_DATA;
                startActivity(ChoiceActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        WallpaperDetailActivity.wallpaperBean = bean;
        startActivity(WallpaperDetailActivity.class);
    }

    @Override
    protected void initData() {
        getTypeData();
        getRecommendData();
        getChoiceData();
    }

    private void getTypeData() {
        StkResApi.getChildTagList(this, AppConfig.TYPE_DATA, StkResApi.createParamMap(0, 3), true, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType.addAll(result);
                    }
                }
            }
        });
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getChoiceData() {
        StkResApi.getTagResourceList(this, AppConfig.CHOICE_DATA, StkResApi.createParamMap(0, 6), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        choiceAdapter.setList(result);
                    }
                }
            }
        });
    }

}